package com.itheima.blog.util;

import com.alibaba.fastjson2.JSON;

import com.itheima.blog.util.entity.EsBlog;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class EsUtils {
    @Autowired
    private RestHighLevelClient client;
    //更新es
    private void insertOrUpdateES(EsBlog esBlog) throws IOException {
        String jsonString = JSON.toJSONString(esBlog);
        IndexRequest indexRequest = new IndexRequest("t_blog").id(esBlog.getId().toString());
        indexRequest.source(jsonString, XContentType.JSON);
        client.index(indexRequest,RequestOptions.DEFAULT);
    }
    //删除blog
    public void delete(String blogId) throws IOException {
        DeleteRequest deleteRequest = new DeleteRequest("t_blog", blogId);
        client.delete(deleteRequest,RequestOptions.DEFAULT);
    }

}
